package com.crm.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.easyui.Grid;
import com.crm.po.Offer;
import com.crm.service.OfferService;
import com.crm.common.CrmInit;
import com.crm.util.RequestEx;
import com.crm.util.ResponseUtils;

public class OfferAction extends Servlet {

	private static final long serialVersionUID = 1116994082953057033L;
	
	@SuppressWarnings("unchecked")
	public void getOfferList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OfferService service = new OfferService();
		Map<String, Object> map = service.getOffers(request);
		
		Grid grid = new Grid();
		grid.setTotal((Integer)map.get("count"));
		grid.setRows((List<Offer>)map.get("rows"));
		
		ResponseUtils.printJson(response, null, grid, null,null);
	}

	public void saveOrUpdateOffer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OfferService service = new OfferService();

		boolean result = service.saveOrUpdateOffer(request);
		
		String value = result ? "ok": "新增或修改数据失败，请检查您的输入数据是否有效。";
		ResponseUtils.printString(response, value);
	}
	
	public void deleteOffer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OfferService service = new OfferService();

		boolean result = service.deleteOffers(request);
		
		String value = result ? "ok": "删除数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void recoverOffer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OfferService service = new OfferService();

		boolean result = service.recoverOffers(request);
		
		String value = result ? "ok": "恢复数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void reallyDeleteOffer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OfferService service = new OfferService();

		boolean result = service.reallyDeleteOffers(request);
		
		String value = result ? "ok": "彻底删除数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void upload(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OfferService service = new OfferService();

		String returnFileName = service.upload(request);
		
		String value = "error".equalsIgnoreCase(returnFileName) ? "error" : returnFileName;
		ResponseUtils.printHtml(response, value);
	}
	
	public void doDownload(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String fileName = RequestEx.getStringTrim(request.getParameter("downloadPath"));

		if (fileName != null && fileName.trim().length() > 0) {		
			try {
				String filedataLocalPath = CrmInit.UPLOAD_PATH + File.separatorChar + fileName;
				FileInputStream fis = new java.io.FileInputStream(filedataLocalPath);
				
				response.setContentType("application/x-download");
				
				String displayName = fileName;
				displayName = URLEncoder.encode(displayName, "UTF-8");
				
				
				response.addHeader("Content-Disposition", "attachment;filename=" + displayName);
				OutputStream os = response.getOutputStream();

				byte[] b = new byte[1024];
				int i = 0;
				while ((i = fis.read(b)) > 0) {
					os.write(b, 0, i);
				}
				fis.close();
				os.flush();
				os.close();

			} catch (Exception e) {
				ResponseUtils.printHtml(response, "文件:【" + fileName + "】不存在或已被删除.");
			}
		}else{
			ResponseUtils.printHtml(response, "下载出现错误,请联系管理员.");
		}
	}

}
